package com.example.dilidili_ting.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dilidili_ting.R;
import com.example.dilidili_ting.utils.OkhttpUtilsTing;
import com.example.dilidili_ting.utils.SharedPreferencesUtilsTing;
import com.example.dilidili_ting.bean.loginResultBeanTing;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.thecode.aestheticdialogs.AestheticDialog;
import com.thecode.aestheticdialogs.DialogStyle;
import com.thecode.aestheticdialogs.DialogType;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class regActivityTing extends AppCompatActivity {

    private RelativeLayout llBarTing;
    private ImageView ivBackTing;
    private EditText etUsernameTing;
    private EditText etPhoneTing;
    private EditText etPasswordTing;
    private EditText etPasswordAgainTing;
    private Button registerBtnTing;
    private TextView tvLoginTing;
    //账号、手机号码、密码、再次输入密码的获取值
    private String userName_ting,psw_ting,pswAgain_ting,phone_ting;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg_ting);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        initView();
    }

    private void initView() {
        etUsernameTing = (EditText) findViewById(R.id.et_username_ting);
        etPhoneTing = (EditText) findViewById(R.id.et_phone_ting);
        etPasswordTing = (EditText) findViewById(R.id.et_password_ting);
        etPasswordAgainTing = (EditText) findViewById(R.id.et_password_again_ting);
        registerBtnTing = (Button) findViewById(R.id.register_btn_ting);
        tvLoginTing = (TextView) findViewById(R.id.tv_login_ting);
        llBarTing = (RelativeLayout) findViewById(R.id.ll_bar_ting);
        ivBackTing = (ImageView) findViewById(R.id.iv_back_ting);
        //标题栏返回功能实现
        ivBackTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                regActivityTing.this.finish();
            }
        });
        registerBtnTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnClick();
            }
        });
        //立即登录
        tvLoginTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(regActivityTing.this, loginActivityTing.class);
                startActivity(intent);
            }
        });
    }

    //获取控件中的字符串
    private void getEditString(){
        userName_ting=etUsernameTing.getText().toString().trim();
        phone_ting=etPhoneTing.getText().toString().trim();
        psw_ting=etPasswordTing.getText().toString().trim();
        pswAgain_ting=etPasswordAgainTing.getText().toString().trim();
    }

    private void btnClick(){
        //获取输入在相应控件中的字符串
        getEditString();
        if (TextUtils.isEmpty(userName_ting)){
            new AestheticDialog.Builder(this, DialogStyle.TOASTER, DialogType.WARNING)
                    .setTitle("WARNING")
                    .setMessage("请输入用户名")
                    .setDuration(1500)
                    .show();
            return;
        }else if (TextUtils.isEmpty(psw_ting)){
            new AestheticDialog.Builder(this, DialogStyle.TOASTER, DialogType.WARNING)
                    .setTitle("WARNING")
                    .setMessage("请输入密码")
                    .setDuration(1500)
                    .show();
            return;
        }else if (TextUtils.isEmpty(pswAgain_ting)){
            new AestheticDialog.Builder(this, DialogStyle.TOASTER, DialogType.WARNING)
                    .setTitle("WARNING")
                    .setMessage("请再次输入密码")
                    .setDuration(1500)
                    .show();
            return;
        }else if (!psw_ting.equals(pswAgain_ting)){
            new AestheticDialog.Builder(this, DialogStyle.TOASTER, DialogType.ERROR)
                    .setTitle("ERROR")
                    .setMessage("输入两次的密码不一致")
                    .setDuration(1500)
                    .show();
            return;
        }else {
            JsonObject jo = new JsonObject();
            jo.addProperty("userName",userName_ting);
            jo.addProperty("pwd",psw_ting);
//            jo.addProperty("kfz","zxt");
            OkhttpUtilsTing.isPOST("AndroidUser/register",false, jo, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {}

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String res = response.body().string();
                    Gson gson = new Gson();
                    final loginResultBeanTing<String> r = gson.fromJson(res, loginResultBeanTing.class);
                    regActivityTing.this.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            if(r.code == 200){
                                new AestheticDialog.Builder(regActivityTing.this, DialogStyle.EMOTION, DialogType.SUCCESS)
                                        .setTitle("SUCCESS")
                                        .setMessage("注册成功")
                                        .setDuration(2000)
                                        .show();
                                //把账号、密码和账号标识保存到sp里面
//                                SharedPreferencesUtilsTing.saveRegisterInfo(regActivityTing.this,userName_ting,psw_ting);
                                //注册成功后把账号传递到loginActivity.java
                                //设置跳转时长
                                Timer timer=new Timer();
                                TimerTask task=new TimerTask() {
                                    @Override
                                    public void run() {
                                        Intent data = new Intent();
                                        data.putExtra("userName",userName_ting);
                                        data.putExtra("password",psw_ting);
                                        setResult(RESULT_OK,data);
                                        regActivityTing.this.finish();
                                    }
                                };
                                timer.schedule(task,2000);
                            }else {
                                new AestheticDialog.Builder(regActivityTing.this, DialogStyle.EMOTION, DialogType.ERROR)
                                        .setTitle("ERROR")
                                        .setMessage(r.msg)
                                        .setDuration(1500)
                                        .show();
                            }
                        }
                    });
                }
            });
        }
    }

}